<template>
  <div>
    <!-- 头部 -->
    <van-nav-bar fixed placeholder>
      <!-- 具名插槽 -->
      <template #left>
        <img class="logo" src="../../assets/toutiao_logo.png" alt="" />
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem" @click="searchFn"></van-icon>
      </template>
    </van-nav-bar>
    <!-- 标签栏 -->
    <!-- sticky 粘性布局  animated 动画切换 -->
    <!-- v-model="active"  active控制高亮 会收集:name="" 里的数据 -->
    <van-tabs v-model="active" animated sticky offset-top="1.22666667rem">
      <van-tab
        v-for="obj in userChannelList"
        :key="obj.id"
        :title="obj.name"
        :name="obj.id"
      >
        <article-list :channel_id="active"></article-list>
      </van-tab>
      <!-- <van-tab title="标签 2">内容 2</van-tab>
      <van-tab title="标签 3">内容 3</van-tab>
      <van-tab title="标签 4">内容 4</van-tab> -->
    </van-tabs>
    <!-- 编辑频道 + -->
    <van-icon
      @click="plusClickFn"
      name="plus"
      size="0.37333334rem"
      class="moreChannels"
    />

    <!-- 一级 反馈面板 -->
    <van-action-sheet
      v-model="$store.state.homeAticleActionShow"
      :actions="actionsArr"
      @select="onSelect"
      get-container="body"
      :cancel-text="cancelText"
      @close='closeActionFn'
      @cancel="cancelFn"
    />

    <!-- 编辑频道 - 弹出层 -->
    <van-popup v-model="show" class="channel_edit_warp">
      <ChannelEdit
       @closePopupEvent = 'closePopupFn' :userChannelList='userChannelList' @addChannelEvent='addChannelFn'
       @removeChannelEvent='removeChannelFn'
       v-model="active"
       ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import {
  getUserChannelListAPI,
  dislikeArticleAPI,
  articleReportsAPI,
  updateChannelAPI,
  removeChannelAPI
} from '@/api'
import ArticleList from './ArticleList.vue'
import { mapState, mapMutations } from 'vuex'
import { Toast } from 'vant'
import ChannelEdit from './ChannelEdit.vue'
export default {
  name: 'Home',
  components: { ArticleList, ChannelEdit },
  data () {
    return {
      active: 0, // 频道ID 不是索引
      userChannelList: [], // 用户已选频道
      actionsArr: [], // 反馈面板选项数组
      cancelText: '取消', // 反馈面板最下面文字
      show: false // 弹出层
    }
  },
  computed: {
    // 映射
    ...mapState(['firstActions', 'secondActions', 'actionArtId'])
  },
  async created () {
    // 获取用户已选频道列表
    const res = await getUserChannelListAPI()
    // console.log(res)
    this.userChannelList = res.data.data.channels
    // 默认反馈面板为一级
    this.actionsArr = this.firstActions
  },
  methods: {
    // 搜索框点击事件
    searchFn () {
      this.$router.push('/search')
    },
    // 删除频道
    async removeChannelFn (channelId) {
      await removeChannelAPI(channelId)
      const index = this.userChannelList.findIndex(obj => obj.id === channelId)
      // 从已选频道数组中删除
      this.userChannelList.splice(index, 1)
    },
    // 添加频道
    async addChannelFn (obj) {
      // obj是组件传来的当前要选的频道 前端要把他加到已选频道里
      // 子组件中未选频道是计算属性, 所以引用变量(userChannelList)改变, 未选频道自动重新计算
      this.userChannelList.push(obj)
      // console.log(this.userChannelList)
      // 先定义一个新数组(前端页面用userChannelList,后端用新数组)
      const newArr = []
      this.userChannelList.forEach((obj, index) => {
        const newObj = { ...obj } // 浅拷贝 得到一个新对象
        newObj.seq = index
        delete newObj.name
        newArr.push(newObj)
      })
      // 重置用户已选频道列表
      await updateChannelAPI(newArr)
    },
    // + 频道编辑的点击事件
    plusClickFn () {
      this.show = true
    },
    // 关闭弹出层
    closePopupFn () {
      this.show = false
    },
    // 点击反馈面板选项
    async onSelect (item) {
      if (item.name === '反馈垃圾内容') {
        // 切换actions数据源为二级反馈内容
        this.actionsArr = this.secondActions

        this.cancelText = '返回' // 改最下方文字
      } else if (item.name === '不感兴趣') {
        const res = await dislikeArticleAPI(this.actionArtId)
        // console.log(res)
        if (res.status === 201) {
          Toast.success('反馈成功') // 提示信息
          this.changeActionShowMutations(false) // 关闭反馈面板
        }
      } else {
        // 点击二级反馈面板内容时触发
        // console.log(item.value)
        try {
          const res = await articleReportsAPI({
            target: this.actionArtId,
            type: item.value,
            remark: '其他问题'
          })
          if (res.status === 201) {
            Toast.success('举报成功')
          }
        } catch (error) {
          Toast.success('举报失败')
        }

        this.changeActionShowMutations(false) // 关闭反馈面板
        setTimeout(() => {
          this.actionsArr = this.firstActions
        }, 200)
      }
    },
    ...mapMutations(['changeActionShowMutations']),
    cancelFn () {
      if (this.cancelText === '返回') {
        this.changeActionShowMutations(true) // 不让弹出层关闭
        this.actionsArr = this.firstActions // 切换为一级反馈面板
        this.cancelText = '取消' // 改最下方文字
      }
    },
    // 反馈面板关闭
    closeActionFn () {
      this.actionsArr = this.firstActions
    }
  },
  destroyed () {
    // 身份过期时 页面离开 底部弹窗可能没关闭
    this.changeActionShowMutations(false) // 关闭反馈面板
  }
}
</script>

<style scoped lang="less">
.logo {
  width: 100px;
  height: 30px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}

.channel_edit_warp {
  background-color: transparent;
  height: 100%;
  width: 100%;
  background-color: white;
}
</style>
